package com.finance.rule.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.finance.base.enums.OpearteEnum;
import com.finance.base.enums.StateEnum;
import com.finance.base.enums.TargetEnum;
import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.rule.bean.pojo.SettingLog;
import com.finance.rule.dao.SettingLogMapper;
import com.finance.system.bean.pojo.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SettingLogService implements ISettingLogService {
    @Autowired
    private SettingLogMapper settingLogMapper;


    @Override
    public SettingLog getById(Long id) {
        return settingLogMapper.getById(id);
    }

    @Override
    public PageResultBO<SettingLog> listByPage(Page page) {
        PageResultBO<SettingLog> pageResultBO = new PageResultBO<SettingLog>();
        List<SettingLog> list = settingLogMapper.listByPage(page);
        pageResultBO.setTotalRecord(page.getTotalRecord());
        pageResultBO.setPageSize(page.getPageSize());
        pageResultBO.setPageNo(page.getPageNo());
        pageResultBO.setDataList(list);
        return pageResultBO;
    }

    @Override
    public boolean add(SettingLog settingLog) {
        return settingLogMapper.add(settingLog) == 1;
    }

    @Override
    public boolean addLog(User user, OpearteEnum opearteEnum, TargetEnum targetEnum, String oldValue) {
        SettingLog settingLog = new SettingLog();
        settingLog.setState(StateEnum.Valid.getState());
        settingLog.setOldValue(oldValue);
        settingLog.setCreateUid(user.getId());
        settingLog.setTarget(targetEnum.getType());

        StringBuffer sb = new StringBuffer();
        sb.append(user.getRealName())
                .append(opearteEnum.getName())
                .append(targetEnum.getName());
        settingLog.setDescription(sb.toString());
        boolean flag = false;
        if (StringUtils.isNotBlank(oldValue)) {
            try {
                JSONObject jsonObject = (JSONObject) JSON.parse(oldValue);
                if (TargetEnum.USER_INFO.getType().equals(targetEnum.getType())) {
                    sb.append("  被变更用户信息的用户工号为: " + jsonObject.getString("code"));
                    settingLog.setDescription(sb.toString());
                }else if(TargetEnum.ROLE_PERMISSION_INFO.equals(targetEnum.getType())){
                    sb.append("  被变更角色信息的角色名称为: "+jsonObject.getString("name"));
                    settingLog.setDescription(sb.toString());
                }else if(TargetEnum.ORGANIZATION_INFO.getType().equals(targetEnum.getType())){
                    sb.append("  被变更机构信息的机构名称为: "+jsonObject.getString("name"));
                    settingLog.setDescription(sb.toString());
                }else if(TargetEnum.SUPPLIER_INFO.getType().equals(targetEnum.getType())){
                    sb.append("  被变更供货商信息的供货商名称为: "+jsonObject.getString("name"));
                    settingLog.setDescription(sb.toString());
                }else if(TargetEnum.PIECE_RANGE_INFO.getType().equals(targetEnum.getType())){
                    sb.append("  被变更按件规则的规则id为: "+jsonObject.getString("id"));
                    settingLog.setDescription(sb.toString());
                }else if(TargetEnum.WEIGHT_RANGE_INFO.getType().equals(targetEnum.getType())){
                    sb.append("  被变更按重规则的规则id为: "+jsonObject.getString("id"));
                    settingLog.setDescription(sb.toString());
                }else if(TargetEnum.MAJOR_RULE_INFO.getType().equals(targetEnum.getType())){
                    sb.append("  被变更首拖件规则的id为: "+jsonObject.getString("id"));
                    settingLog.setDescription(sb.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                flag = settingLogMapper.add(settingLog) == 1;
            }
        }
        return flag;
    }

    @Override
    public void addLog(User user, TargetEnum targetEnum, String description){
        SettingLog settingLog = new SettingLog();
        settingLog.setState(StateEnum.Valid.getState());
        settingLog.setCreateUid(user.getId());
        settingLog.setTarget(targetEnum.getType());
        settingLog.setDescription(description);
        settingLogMapper.add(settingLog);
    }
}
